package com.mobi.cacti.common;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import com.mobi.cacti.rabbit.response.GraphCircuit;

public class ConvertXml {
		
	public static String cCircuitMapToStrXML(Map<String, GraphCircuit> circuitMap, String rootName, String elemName)
    {
		String xmlStr = "<" + rootName + ">";

		for( String key: circuitMap.keySet() ){
			xmlStr += "<" + elemName + ">" + circuitMap.get(key).CircuitID.toString() +"</" + elemName + ">";
		}

        xmlStr += "</" + rootName + ">";

        return xmlStr;
    }
	
	public static Map<String, String> convertNodesFromXml(String xml) throws Exception {

	    InputStream is = new ByteArrayInputStream(xml.getBytes());
	    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	    dbf.setNamespaceAware(true);
	    DocumentBuilder db = dbf.newDocumentBuilder();
	    Document document = db.parse(is);
	    return createMap(document.getDocumentElement());
	}
	public static Map<String, String> createMap(Node node) {
	    Map<String, String> map = new HashMap<String, String>();
	    NodeList nodeList = node.getChildNodes();
	    for (int i = 0; i < nodeList.getLength(); i++) {
	        Node currentNode = nodeList.item(i);
	        if (currentNode.hasAttributes()) {
	            for (int j = 0; j < currentNode.getAttributes().getLength(); j++) {
	                Node item = currentNode.getAttributes().item(i);
	                map.put(item.getNodeName(), item.getTextContent());
	            }
	        }
	        if (node.getFirstChild() != null && node.getFirstChild().getNodeType() == Node.ELEMENT_NODE) {
	            map.putAll(createMap(currentNode));
	        } else if (node.getFirstChild().getNodeType() == Node.TEXT_NODE) {
	            map.put(node.getLocalName(), node.getTextContent());
	        }
	    }
	    return map;
	}
		
	public static Document convertStrXmlToDoc(String xml) throws Exception
	{
		try
		{
		   DocumentBuilderFactory docbf = DocumentBuilderFactory.newInstance();
		   DocumentBuilder docb = docbf.newDocumentBuilder();
		   InputSource insrc = new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8")));
		   
		   /*
		   Document doc = docb.parse(insrc);
		   DOMSource domSource = new DOMSource(doc);
	       StringWriter writer = new StringWriter();
	       StreamResult result = new StreamResult(writer);
	       TransformerFactory tf = TransformerFactory.newInstance();
	       Transformer transformer = tf.newTransformer();
	       transformer.transform(domSource, result);
	       */
	            
		   return docb.parse(insrc);
		}
		catch(Exception ex)
		{
			return null;
		}
	}
	public static String convertDocToStr(Document doc)
	{
		try
		{
			DOMSource dom = new DOMSource(doc);
		    StringWriter writer = new StringWriter();  
		    StreamResult result = new StreamResult(writer);

		    TransformerFactory factory = TransformerFactory.newInstance();
		    Transformer transformer = factory.newTransformer();
		    transformer.transform(dom, result);

		    return writer.toString();
		}
		catch(Exception ex)
		{
			return "";
		}
	}

	
}
